//
//  RegisterServerViewController.m
//  跨界收
//
//  Created by Allen on 2018/10/27.
//  Copyright © 2018年 gztx. All rights reserved.
//

#import "RegisterServerViewController.h"
#import "YKPickerManager.h"

@interface RegisterServerViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *companyTextField;
@property (weak, nonatomic) IBOutlet UITextField *cityTextField;

@property (weak, nonatomic) IBOutlet UIButton *shanghuziyuanBtn;
@property (weak, nonatomic) IBOutlet UIButton *chuangyehezuoBtn;
@property (weak, nonatomic) IBOutlet UIButton *yidongzhifuBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewoneConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTwoContraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewThreeContraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewFourConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *submitYConstairnt;

@property (nonatomic, strong) NSString *zipcode;

@end

@implementation RegisterServerViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"申请成为服务商";
    self.cityTextField.delegate = self;
    self.phoneTextField.delegate = self;
    self.companyTextField.delegate = self;
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if (SCREEN_WIDTH < 375.0) {
        self.viewoneConstraint.constant = 45;
        self.viewTwoContraint.constant = 45;
        self.viewThreeContraint.constant = 45;
        self.viewFourConstraint.constant = 160;
        self.submitYConstairnt.constant = 40;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
}

- (BOOL)isValidInput
{
    if (self.companyTextField.text.length == 0) {
        [self showInfo:@"请输入公司/个人名称"];
        return NO;
    }
    if (self.phoneTextField.text.length == 0) {
        [self showInfo:@"请输入手机号"];
        return NO;
    }
    if (! [Tool IsPhoneNumber:self.phoneTextField.text]) {
        [self showInfo:@"请输入正确的手机号"];
        return NO;
    }
    if (self.cityTextField.text.length == 0) {
        [self showInfo:@"请选择城市"];
        return NO;
    }
    if (!self.shanghuziyuanBtn.selected && !self.chuangyehezuoBtn.selected && !self.yidongzhifuBtn.selected) {
        [self showInfo:@"请选择合作意向"];
        return NO;
    }
    return YES;
}

- (IBAction)click:(UIButton *)sender
{
    sender.selected = !sender.selected;
    switch (sender.tag) {
        case 100:
        {
            
            if (sender.selected) {
                self.yidongzhifuBtn.selected = NO;
                self.chuangyehezuoBtn.selected = NO;
            }
        }
            break;
        case 101:
        {
            if (sender.selected) {
                self.shanghuziyuanBtn.selected = NO;
                self.yidongzhifuBtn.selected = NO;
            }
        }
            break;
        case 102:
        {
            if (sender.selected) {
                self.shanghuziyuanBtn.selected = NO;
                self.chuangyehezuoBtn.selected = NO;
            }
        }
            break;
        default:
            break;
    }
    
}

- (void)closeKeyboard
{
    [self.companyTextField resignFirstResponder];
    [self.phoneTextField resignFirstResponder];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == self.cityTextField) {
        [self closeKeyboard];
        [[YKPickerManager shareManager] showAddressPickerView:[UIColor redColor] defaultAddress:@"" commitBlock:^(NSString *address, NSString *zipcode) {
            self.cityTextField.text = address;
            self.zipcode = zipcode;
        } cancelBlock:^{ }];
        return NO;
    } else {
        return YES;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self closeKeyboard];
    return YES;
}

- (IBAction)submit:(id)sender
{
    if (! [self isValidInput]) {
        return;
    }
    [self closeKeyboard];
    [self reqeustForSubmit];
}

#pragma mark - Request
- (void)configProvince:(NSString **)province city:(NSString **)city area:(NSString **)area intention:(int *)intention
{
    NSArray *zipcodeArray = [self.zipcode componentsSeparatedByString:@"-"];
    if (zipcodeArray.count >= 3) {
        *province = zipcodeArray[0];
        *city = zipcodeArray[1];
        *area = zipcodeArray[2];
    }
    if (self.shanghuziyuanBtn.selected) {
        *intention = 1;
    }
    if (self.chuangyehezuoBtn.selected) {
        *intention = 2;
    }
    if (self.yidongzhifuBtn.selected) {
        *intention = 3;
    }
}

- (void)reqeustForSubmit
{
    NSString *province = @"";
    NSString *city = @"";
    NSString *area = @"";
    int intention = 3;
    [self configProvince:&province city:&city area:&area intention:&intention];
    NSDictionary *parameters = @{ @"name": self.companyTextField.text,
                                  @"mobile": self.phoneTextField.text,
                                  @"province": province,
                                  @"city": city,
                                  @"area": area,
                                  @"intention": @(intention) };
    
    [self showHUD];
    [NetworkManager post:kApplyAgentApi parameters:parameters success:^(id data) {
        [self hideHUD];
        [self showSuccess:@"申请成功"];
    } failure:^(NSError *error) {
        [self hideHUD];
        [self handleRequestFailure:error];
    }];
}

@end
